﻿
using Mono.Cecil;
public class LuaMethodReference : LuaMemberReference<MethodReference>
{
    private readonly LuaTypeReference _type;

    private readonly ObjectKind _returnType;
    private readonly IReadOnlyList<LuaArgDefinition> _parameters;
  
    public virtual bool IsConstructor => false;
    public LuaMethodReference(LuaModuleDefinition module, MethodReference self) : base(module, self)
    {
        if (self.IsGenericInstance)
        {
            Name = ToGenericName(self.Name);
            FullName = ToGenericFullName(self.FullName);
        }
        else if (self.Name == ".ctor")
            Name = "__init__";
        _returnType = Resolve().ReturnType.ToStackType();
        _parameters = new List<LuaArgDefinition>(from arg in self.Parameters select new LuaArgDefinition(module, this, arg));
        
        _type = GetTypeReference(Resolve().DeclaringType);
    }

    public bool HasSelf
        => Resolve().HasThis;

    public ObjectKind ReturnType
        => _returnType;

    public IReadOnlyList<LuaArgDefinition> Args
        => _parameters;

    public LuaTypeReference Type
        => _type;
}

